package com.mes.web.controller.biz;

import cn.hutool.core.date.DateUtil;
import com.mes.biz.service.BizCustomerPayableDetailService;
import com.mes.biz.service.BizOrderService;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 首页Controller
 * @author hwg
 * @email hwg
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/biz/index")
public class BizIndexController extends BaseController {

    @Autowired
    private BizOrderService bizOrderService;
    @Autowired
    private BizCustomerPayableDetailService bizCustomerPayableDetailService;

    /**
     * 统计数据
     */
    @GetMapping("/data")
    public R<Map<String,Object>> getStatData() {
        //查询今日销售单数量、金额
        Map<String,Object> todaySaleOrderData = bizOrderService.getSaleOrderDataByToday();
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("todaySaleOrderData",todaySaleOrderData);
        //查询今日生产数
        Map<String,Object> todayProductData = bizOrderService.getProductDataByToday();
        resultMap.put("todayProductData",todayProductData);
        //查询今日完成数
        Map<String,Object> todayCompleteData = bizOrderService.getCompleteDataByToday();
        resultMap.put("todayCompleteData",todayCompleteData);
        //查询今日派单数量
        Map<String,Object> todayDispatchData = bizOrderService.getDispatchDataByToday();
        resultMap.put("todayDispatchData",todayDispatchData);
        //查询今日安装数量
        Map<String,Object> todayInstallerData = bizOrderService.getInstallerDataByToday();
        resultMap.put("todayInstallerData",todayInstallerData);
        //查询今日测量数量
        Map<String,Object> todayMeasureData = bizOrderService.getMeasureDataByToday();
        resultMap.put("todayMeasureData",todayMeasureData);

        //查询今日收款数据
        Map<String,Object> todayIncomeData = bizCustomerPayableDetailService.getIncomeDataByToday();
        resultMap.put("todayIncomeData",todayIncomeData);
        //查询本月日销售单金额
        //查询本月天数
        int thisMonthDays = DateUtil.thisDayOfMonth();
        int monthDays = 0;
        if(thisMonthDays<31) {
            //查询上月天数
            int lastMonthDays = DateUtil.endOfMonth(DateUtil.lastMonth()).dayOfMonth();
            if(thisMonthDays<lastMonthDays) {
                monthDays=lastMonthDays;
            } else {
                monthDays=thisMonthDays;
            }
        }
        //生成天数数据
        List<Integer>  dayList = new ArrayList<>();
        List<Map<String,Object>> thisMonthOrderAmountData = bizOrderService.getThisMonthOrderAmountData();
        List<Map<String,Object>> lastMonthOrderAmountData = bizOrderService.getLastMonthOrderAmountData();
        for(int i=1;i<=monthDays;i++) {
            dayList.add(i);
            boolean existsMonthOrderAmount=false;
            boolean existsLashMonthOrderAmount=false;
            if(thisMonthDays>=i) {
                for(Map<String,Object> thisMonthOrderAmountDataMap:thisMonthOrderAmountData) {
                    if(i == Integer.valueOf(String.valueOf(thisMonthOrderAmountDataMap.get("receiveDate"))).intValue()) {
                        existsMonthOrderAmount=true;
                        break;
                    }
                }
                if(!existsMonthOrderAmount) {
                    Map<String,Object> thisMonthOrderAmountDataMap = new HashMap<>();
                    thisMonthOrderAmountDataMap.put("receiveDate",i);
                    thisMonthOrderAmountDataMap.put("saleAmount",0);
                    thisMonthOrderAmountData.add(i-1,thisMonthOrderAmountDataMap);
                }
            }
            for(Map<String,Object> lastMonthOrderAmountDataMap:lastMonthOrderAmountData) {
                if(i == Integer.valueOf(String.valueOf(lastMonthOrderAmountDataMap.get("receiveDate"))).intValue()) {
                    existsLashMonthOrderAmount=true;
                    break;
                }
            }
            if(!existsLashMonthOrderAmount) {
                Map<String,Object> lastMonthOrderAmountDataMap = new HashMap<>();
                lastMonthOrderAmountDataMap.put("receiveDate",i);
                lastMonthOrderAmountDataMap.put("saleAmount",0);
                lastMonthOrderAmountData.add(i-1,lastMonthOrderAmountDataMap);
            }
        }
        List<Object> thisMonthOrderAmountDataList = new ArrayList<>();
        for(Map<String,Object> thisMonthOrderAmountDataMap:thisMonthOrderAmountData) {
            thisMonthOrderAmountDataList.add(thisMonthOrderAmountDataMap.get("saleAmount"));
        }
        List<Object> lastMonthOrderAmountDataList = new ArrayList<>();
        for(Map<String,Object> lastMonthOrderAmountDataMap:lastMonthOrderAmountData) {
            lastMonthOrderAmountDataList.add(lastMonthOrderAmountDataMap.get("saleAmount"));
        }
        resultMap.put("thisMonthDays",dayList);
        resultMap.put("thisMonthOrderAmountData",thisMonthOrderAmountDataList);
        resultMap.put("lastMonthOrderAmountData",lastMonthOrderAmountDataList);

        //查询月份
        List<Integer> monthList = new ArrayList<>();
        int thisMonth = DateUtil.thisMonth()+1;
        //查询每月销售数量
        List<Map<String,Object>> monthSaleOrderData = bizOrderService.getSaleOrderDataByMonth();
        for(int i=1;i<=thisMonth;i++) {
            monthList.add(i);
            boolean existsMonthOrderCount=false;
            if(thisMonthDays>=i) {
                for(Map<String,Object> monthOrderCountDataMap:monthSaleOrderData) {
                    if(i == Integer.valueOf(String.valueOf(monthOrderCountDataMap.get("receiveDate"))).intValue()) {
                        existsMonthOrderCount=true;
                        break;
                    }
                }
                if(!existsMonthOrderCount) {
                    Map<String,Object> monthOrderCountDataMap = new HashMap<>();
                    monthOrderCountDataMap.put("receiveDate",i);
                    monthOrderCountDataMap.put("monthCount",0);
                    monthSaleOrderData.add(i-1,monthOrderCountDataMap);
                }
            }
        }
        resultMap.put("monthList",monthList);
        List<Object> monthSaleOrderDataList = new ArrayList<>();
        for(Map<String,Object> monthSaleOrderDataMap:monthSaleOrderData) {
            monthSaleOrderDataList.add(monthSaleOrderDataMap.get("monthCount"));
        }
        resultMap.put("monthSaleOrderData",monthSaleOrderDataList);

        //查询本月收款金额
        BigDecimal monthSaleOrderAmount = bizOrderService.getSaleOrderAmountByMonth(DateUtil.format(new Date(),"YYYY-MM"));
        //查询上月收款金额
        BigDecimal lastMonthSaleOrderAmount = bizOrderService.getSaleOrderAmountByMonth(DateUtil.format(DateUtil.lastMonth(),"YYYY-MM"));
        //查询上上月收款金额
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-2);
        BigDecimal last2MonthSaleOrderAmount = bizOrderService.getSaleOrderAmountByMonth(DateUtil.format(calendar.getTime(),"YYYY-MM"));
        resultMap.put("monthSaleOrderAmount",monthSaleOrderAmount);
        if(monthSaleOrderAmount.compareTo(BigDecimal.ZERO)==0) {
            resultMap.put("incMonthSaleOrderAmountRatio",0);
        } else {
            resultMap.put("incMonthSaleOrderAmountRatio",
                    lastMonthSaleOrderAmount.compareTo(BigDecimal.ZERO)==0?100:
                            monthSaleOrderAmount.subtract(lastMonthSaleOrderAmount).divide(lastMonthSaleOrderAmount,4,BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)));
        }
        resultMap.put("incMonthSaleOrderAmount",monthSaleOrderAmount.subtract(lastMonthSaleOrderAmount));
        resultMap.put("inc2MonthSaleOrderAmount",lastMonthSaleOrderAmount.subtract(last2MonthSaleOrderAmount));
        return R.data(resultMap);
    }

    public static void main(String[] args) {
        System.err.println(DateUtil.lastMonth().month());
    }
}
